//
//  AllOrderViewController.m
//  Restaurant
//
//  Created by wanghuan on 15/5/5.
//  Copyright (c) 2015年 wanghuan. All rights reserved.
//

#import "AllOrderViewController.h"

@interface AllOrderViewController ()

@end

@implementation AllOrderViewController

- (void)viewWillAppear:(BOOL)animated {
    
    [super viewWillAppear:animated];
    
    _requestManager = [[WHAFHTTPRequest alloc]init];
    
    [_dataArr removeAllObjects];
    
    
    _requestManager.Sblock = ^(id obj) {
        
        
        
        [self downLoad:obj];
        
    };
    
    _requestManager.Fblock = ^(NSString *error) {
        
        
        
    };
    
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    
    if ([defaults objectForKey:usersId]) {
        
         [_requestManager createWHAFHttpRequestUrl:[NSString stringWithFormat:AllOrderUrl,[defaults objectForKey:usersId]]];
        
    }else {
    
        [_OrderTbaleView reloadData];
    }
   


}

- (void)viewDidLoad {
    
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    
    self.view.backgroundColor = AppBackGroundColor;
    
    _dataArr = [[NSMutableArray alloc]init];
    
    _OrderTbaleView = [[UITableView alloc]initWithFrame:CGRectMake(0, 66, APPWith, APPHeight-64-49) style:UITableViewStyleGrouped];
    _OrderTbaleView.delegate = self;
    _OrderTbaleView.dataSource = self;
    [self.view addSubview:_OrderTbaleView];
    
}

#pragma mark - 解析数据

- (void)downLoad:(id)data {
    
    [_dataArr removeAllObjects];
    
    if (![[data objectForKey:@"success"]boolValue]) {
        
        UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, APPWith, 50)];
        
        label.text = [data objectForKey:@"code"];
        
        label.center = _OrderTbaleView.center;
        
        label.textAlignment = NSTextAlignmentCenter;
        
        [_OrderTbaleView addSubview:label];
        
        return;
    }
    
    
    NSArray *ObjArr = [data objectForKey:@"obj"];
        
        for (NSDictionary *ObjDic in [data objectForKey:@"obj"]) {
            
            AllOrderModel *model = [[AllOrderModel alloc]init];
            model.isComment = [NSString stringWithFormat:@"%@",[ObjDic objectForKey:@"isComment"]];
            model.time = [NSString stringWithFormat:@"%@",[ObjDic objectForKey:@"time"]];
            model.totalprice = [NSString stringWithFormat:@"%@",[ObjDic objectForKey:@"totalprice"]];
            model.merchantName = [NSString stringWithFormat:@"%@",[ObjDic objectForKey:@"merchantName"]];
            model.orderId = [NSString stringWithFormat:@"%@",[ObjDic objectForKey:@"orderId"]];
            
            [_dataArr addObject:model];
        }
        
        [_OrderTbaleView reloadData];

}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {

    return 40;

}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {

    return _dataArr.count;

}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {

    return 2;

}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
     AllOrderModel *model = [_dataArr objectAtIndex:indexPath.section];

    if (!indexPath.row) {
        
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"title"];
        
        UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(20, 10, 150, 20)];
        
        UILabel *dateLabel = [[UILabel alloc]initWithFrame:CGRectMake(APPWith-140, 10, 150, 20)];
        
        if (!cell) {
            
            cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"title"];
            label.textColor = [UIColor blackColor];
            label.font = [UIFont systemFontOfSize:17];
            [cell.contentView addSubview:label];
            dateLabel.textColor = [UIColor grayColor];
            dateLabel.font = [UIFont systemFontOfSize:11];
            
            [cell.contentView addSubview:dateLabel];
            
            cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;

        }
        
        label.text = model.merchantName;
        
        dateLabel.text = model.time;
        
        return cell;

    }
    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell1"];
    
    UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(20, 10, 100, 20)];
    
    UIButton *button = [[UIButton alloc]initWithFrame:CGRectMake(APPWith-80, 5, 70, 30)];
    
    if (!cell) {
        
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell1"];
        
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        label.textColor = [UIColor blackColor];
        label.font = [UIFont systemFontOfSize:14];
        [cell.contentView addSubview:label];
        
        button.layer.borderColor = [[UIColor redColor]CGColor];
        button.layer.borderWidth = 1;
        button.layer.cornerRadius = 10;
        button.titleLabel.font = [UIFont systemFontOfSize:13];
        [button setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
        [button addTarget:self action:@selector(buttonClick:) forControlEvents:UIControlEventTouchUpInside];
        [cell.contentView addSubview:button];

    }
    
    label.text = [NSString stringWithFormat:@"￥%@",model.totalprice];
    
    if ([model.isComment isEqual:@"1"]) {
        
        [button setTitle:@"已评价" forState:UIControlStateNormal];
        
    } else {
        
        [button setTitle:@"去评价" forState:UIControlStateNormal];
    }
    
    
    return cell;

    

}


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {

    if (!indexPath.row) {
        
        AllOrderDetailViewController *Aodv = [[AllOrderDetailViewController alloc]init];
        
        Aodv.allOrderModel = [_dataArr objectAtIndex:indexPath.section];
        
        [self.navigationController pushViewController:Aodv animated:YES];
        
        
    }
    
    [tableView deselectRowAtIndexPath:indexPath animated:YES];

    return;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {

    return 2;

}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {

    return 2;
}

//- (void)viewWillDisappear:(BOOL)animated {
//
//    [super viewWillDisappear:animated];
//    
//    NSLog(@"%@",_dataArr);
//
//}

#pragma mark - 去评价的点击事件

- (void)buttonClick:(UIButton *)sender {

    ReviewViewController *rvc = [[ReviewViewController alloc]init];
    
    [self.navigationController pushViewController:rvc animated:YES];

}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
